import {
  getAdminList,
  login,
  register,
  logout,
  info,
  getGroups,
  postGroup,
  putGroup,
  deleteGroup,
  getRoles,
  postRoles,
  putRoles,
  deleteRoles,
  putPassword
} from '@/api/admin'
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
import {
  resetRouter
} from '@/router'
import Layout from '@/layout'
const getDefaultState = () => {
  return {
    token: getToken(),
    groups: [],
    avatar: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_AVATAR: (state, v) => {
    state.avatar = v
  },
  SET_GROUPS: (state, v) => {
    state.groups = v
  }
}

const actions = {
  // user login
  login({
    commit
  }, userInfo) {
    const {
      username,
      password
    } = userInfo
    return new Promise((resolve, reject) => {
      login({
        username: username.trim(),
        password: password
      }).then(response => {
        // console.log(response.data)
        commit('SET_TOKEN', response.data.token)
        setToken(response.data.token)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  register({
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      register(data).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  putPassword({
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      putPassword(data).then(response => {
        commit('SET_TOKEN', response.data.token)
        setToken(response.data.token)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  getAdminList({
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      getAdminList(data).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  info({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      info().then(response => {
        // console.log(response.data)
        commit('SET_GROUPS', response.data.groups)
        commit('SET_AVATAR', response.data.avatar)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  getUserList({
    commit,
    state
  }, data) {
    return new Promise((resolve, reject) => {
      getUserList(data).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  getGroups({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      getGroups(state.token).then(response => {
        // commit('SET_AVATAR', avatar)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  getRoles({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      getRoles(state.token).then(response => {
        // commit('SET_AVATAR', avatar)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  postGroup({
    commit,
    state
  }, data) {
    return new Promise((resolve, reject) => {
      postGroup(data).then(response => {
        // commit('SET_AVATAR', avatar)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  deleteGroup({
    commit,
    state
  }, data) {
    return new Promise((resolve, reject) => {
      deleteGroup(data).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  putGroup({
    commit,
    state
  }, data) {
    return new Promise((resolve, reject) => {
      putGroup(data).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  postRoles({
    commit,
    state
  }, data) {
    return new Promise((resolve, reject) => {
      postRoles(data).then(response => {
        // commit('SET_AVATAR', avatar)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  deleteRoles({
    commit,
    state
  }, data) {
    return new Promise((resolve, reject) => {
      deleteRoles(data).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  putRoles({
    commit,
    state
  }, data) {
    return new Promise((resolve, reject) => {
      putRoles(data).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  logout({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken()
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  resetToken({
    commit
  }) {
    return new Promise(resolve => {
      removeToken()
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
